import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import {
	login,
	logout,
	getUserInfo
} from '@/api/login'
import {
	getToken,
	setToken,
	removeToken
} from '@/utils/auth'

const baseUrl = config.baseUrl

const user = {
	state: {
		// token: getToken(),
		uid: storage.get(constant.uid),
		mobile: storage.get(constant.mobile),
		nickname: storage.get(constant.nickname),
		avatar: storage.get(constant.avatar),
		school: storage.get(constant.school),
		birthday: storage.get(constant.birthday),
		gender: storage.get(constant.gender)
	},

	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token
		},
		SET_UID: (state, uid) => {
			state.uid = uid
			storage.set(constant.uid, uid)
		},
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar
			storage.set(constant.avatar, avatar)
		},
		SET_MODILE: (state, mobile) => {
			state.mobile = mobile
			storage.set(constant.mobile, mobile)
		},
		SET_NICKNSME: (state, nickname) => {
			state.nickname = nickname
			storage.set(constant.nickname, nickname)
		},
		SET_SCHOOL: (state, school) => {
			state.school = school
			storage.set(constant.school, school)
		},
		SET_BIRTHDAY: (state, birthday) => {
			state.birthday = birthday
			storage.set(constant.birthday, birthday)
		},
		SET_GENDER: (state, gender) => {
			state.gender = gender
			storage.set(constant.gender, gender)
		}
	},

	actions: {
		// 登录
		Login({
			commit
		}, userInfo) {
			return new Promise((resolve, reject) => {
				login(userInfo).then(res => {
					setToken(res.data.remark)
					// commit('SET_TOKEN', res.token)
					if (res.code == 200) {
						commit('SET_UID', res.data.uid)
						commit('SET_AVATAR', res.data.avatar)
						commit('SET_NICKNSME', res.data.nickname)
					}

					resolve(res.code)
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 获取用户信息
		GetInfo({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				getUserInfo().then(res => {
					commit('SET_UID', res.data.uid)
					commit('SET_AVATAR', res.data.avatar)
					commit('SET_MODILE', res.data.mobile)
					commit('SET_NICKNSME', res.data.nickname)
					commit('SET_SCHOOL', res.data.school)
					commit('SET_BIRTHDAY', res.data.birthday)
					commit('SET_GENDER', res.data.gender)
					resolve(res)
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 退出系统
		LogOut({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				logout().then(res => {
					removeToken()
					commit('SET_UID', '')
					commit('SET_AVATAR', '')
					commit('SET_MODILE', '')
					commit('SET_NICKNSME', '')
					commit('SET_SCHOOL', '')
					commit('SET_BIRTHDAY', '')
					commit('SET_GENDER', '')
					storage.clean()

					// removeToken()

					resolve(res.code)
				}).catch(error => {
					reject(error)
				})
			})
		},


		// 退出系统
		ClearOut({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				removeToken()
				commit('SET_UID', '')
				commit('SET_AVATAR', '')
				commit('SET_MODILE', '')
				commit('SET_NICKNSME', '')
				commit('SET_SCHOOL', '')
				commit('SET_BIRTHDAY', '')
				commit('SET_GENDER', '')
				storage.clean()
				resolve()
			}).catch(error => {
				reject(error)
			})
		}
	}
}

export default user
